#!/usr/bin/python
#-*- coding: utf-8 -*-

import re, sqlite3

mesh_file = open("d2013.bin", 'r')
mesh_hash = {}

for line in mesh_file.readlines():
    name_match = re.search(r'^MH = (.+)$', line.strip())
    if name_match:
        name = name_match.group(1)
    
    number_match = re.search(r'^MN = (.+)$', line.strip())
    if number_match:
        number = number_match.group(1)
        mesh_hash[number] = name

mesh_file.close()

con = sqlite3.connect("mesh_sqlite.db")
cur = con.cursor()

for key in mesh_hash.keys():
    entry = (key, mesh_hash[key])
    cur.execute("INSERT INTO mesh (MN, MH) VALUES (?, ?)", entry)
    
con.commit()
con.close()